import ajaxAsync from 'vue-xiaobu-utils'

// import ajaxAsync from 'vue-xiaobu-utils'

// import ajaxAsync from 'vue-xiaobu-utils'

// import ajaxAsync from 'vue-xiaobu-utils'

/**
 * 获取云卡申请的身份信息
 *
 * @returns
 */
async function getVCardApplyInfo() {
  let rs = await ajaxAsync('/vcard/applyinfo/get', {})
  return [rs.CARD_OWNER, rs.ID_CARD_NUMBER]
}

/**
 *商户侧绑定银行卡发送短信
 *
 * @param {String} phoneNo 手机号
 * @param {String} cardNo 银行卡号(明)
 * @returns
 */
async function sendBindCardMsg(phoneNo, cardNo) {
  let rs = await ajaxAsync('/bindcard/bind/msg', {
    PHONE_NO: phoneNo,
    BANK_CARD_NO: cardNo
  })
  return rs.BINDCARD_ORDER_NO
}

/**
 *绑定储蓄卡
 *
 * @param {String} smsCode 短信校验码
 * @param {String} bindCardOrderNo 绑卡订单号
 * @param {String} phoneNo 手机号
 * @param {String} bankCardNo 银行卡号(明)
 */
async function bindDebitCard(smsCode, bindCardOrderNo, phoneNo, bankCardNo) {
  let rs = await ajaxAsync('/bindcard/bind', {
    SMS_CODE: smsCode,
    BINDCARD_ORDER_NO: bindCardOrderNo,
    PHONE_NO: phoneNo,
    BANK_CARD_NO: bankCardNo,
    CARD_TYPE: 1
  })
  return rs.BIND_STATUS === 1
}

/**
 *绑定信用卡
 *
 * @param {String} smsCode 短信校验码
 * @param {String} bindCardOrderNo 绑卡订单号
 * @param {String} phoneNo 手机号
 * @param {String} bankCardNo 银行卡号(明)
 * @param {String} cvn2 贷记卡cvn2
 * @param {String} expireDate 贷记卡有效期，yyMM(年后两位加月份)商户侧签约信用卡必输
 */
async function bindCreditCard(
  smsCode,
  bindCardOrderNo,
  phoneNo,
  bankCardNo,
  cvn2,
  expireDate
) {
  let rs = await ajaxAsync('/bindcard/bind', {
    SMS_CODE: smsCode,
    BINDCARD_ORDER_NO: bindCardOrderNo,
    PHONE_NO: phoneNo,
    BANK_CARD_NO: bankCardNo,
    CARD_TYPE: 2,
    CVN2: cvn2,
    EXPIRE_DATE: expireDate
  })
  return rs.BIND_STATUS === 1
}

/**
 *检查用户是否有未支付成功的行程
 *
 * @returns Promise
 */
async function checkUnpaySuccessBill() {
  let rs = await ajaxAsync('/posbill/unpaysuccess/count', {})
  let count = rs.UNPAY_SUCCESS_COUNT

  return new Promise(function(resolve, reject) {
    if (count === 0) {
      resolve()
    } else {
      reject(new Error('抱歉，您有' + count + '笔支付失败行程未处理'))
    }
  })
}

/**
 *检查用户是否有未解绑的银行卡
 *
 * @returns Promise
 */
async function checkUnBindBankCard() {
  let rs = await ajaxAsync('/bindcard/list', {})
  let count = rs.COUNT
  return new Promise(function(resolve, reject) {
    if (count === 0) {
      resolve()
    } else {
      reject(new Error('您有当前身份信息下的银行卡未解绑'))
    }
  })
}

/**
 *发送通用短信，用于身份变更时，验证身份用
 *
 * @param {String} phoneno 手机号码
 * @returns
 */
async function sendSmsCode(phoneno) {
  return ajaxAsync('/user/smscode/get', { PHONE: phoneno })
}

/**
 *云卡申请的身份信息变更验证码校验
 *
 * @param {String} phoneno 手机号码
 * @param {String} smsCode 手机验证码
 * @returns 验证码校验上下文KEY
 */
async function checkUpdateApplyInfo(phoneno, smsCode) {
  let rs = await ajaxAsync('/vcard/applyinfo/update/check', {
    PHONE: phoneno,
    SMS_CODE: smsCode
  })
  return rs.CONTEXT_KEY
}

export {
  getVCardApplyInfo,
  sendBindCardMsg,
  bindDebitCard,
  bindCreditCard,
  checkUnpaySuccessBill,
  checkUnBindBankCard,
  sendSmsCode,
  checkUpdateApplyInfo
}
